package app.nbdevices.service.impl;

import app.nbdevices.modle.appModle.RegisterModle;
import app.nbdevices.modle.appModle.TaskDetailModle;
import app.nbdevices.modle.appModle.TaskResModle;
import app.nbdevices.service.INbDevService;
import app.nbdevices.utils.HttpsUtil;
import app.nbdevices.utils.RegisterUtil;
import app.nbdevices.utils.StringUtil;
import app.nbdevices.vo.RegisterInfoVo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import refactor.entity.Device;
import refactor.mapper.DeviceMapper;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

@Service
public class NbDevServiceImpl implements INbDevService {

        @Autowired
        DeviceMapper deviceMapper;

        @Autowired
        @Qualifier("HttpsUtil")
        HttpsUtil httpsUtil;

        @Override
        public int addDev(RegisterInfoVo vo) {
            Device d=new Device();
            d.setFacilitiesId(0);
            d.setOnline(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
            d.setEndingTime(0l);
            d.setModId(vo.getModId());
            d.setAddrId(vo.getAddrId());
            d.setInternetDeviceId(vo.getDevInternetId());
            d.setCode(vo.getDevCode());
            d.setName(vo.getName());
            d.setStandard("");

            System.out.println(d);
            return 0;
            //return deviceMapper.insert(d);
        }

    @Override
    public int lotAddDev(MultipartFile mFile, int id) throws Exception {
            List<RegisterModle> infoList=RegisterUtil.Xls2PojoList(mFile);
            for(RegisterModle registerModle:infoList){
                System.out.println(registerModle);
            }
            TaskResModle taskResModle= RegisterUtil.regRequest(infoList,id, StringUtil.getRandomString(6),httpsUtil);
            List<TaskDetailModle> details=taskResModle.getTaskDetails();
            List<Device> devs=new ArrayList<>(infoList.size());
            for(int i=0;i<infoList.size();i++){
                TaskDetailModle dtm=details.get(i);
                if(dtm.getStatus().equals("Success")){
                    Device d=new Device();
                    int index=dtm.getParam().getIndex();
                   d.setInternetDeviceId(dtm.getOutput().substring(13,49));
                   d.setAddrId(infoList.get(index).getAddrId());
                   d.setName(infoList.get(index).getName());
                   d.setStandard("");
                   d.setCode(infoList.get(index).getAddrId());
                   d.setModId(id);
                   d.setEndingTime(0l);
                   d.setOnline(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
                   d.setFacilitiesId(0);
                   d.setLevel(0);
                   devs.add(d);
                }else if(dtm.getStatus().equals("error")){

                }else{

                }
            }
            if(devs.size()!=0){
                deviceMapper.lotInsertDevice(devs);
            }
            return 0;
     }
}
